package com.buaa.controller;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buaa.service.ResultService;
import com.buaa.utils.PieChart3D;

@Controller
@RequestMapping("result")
public class ResultController {

	@Autowired
	private ResultService resultService;

	/**
	 * 统计信息界面
	 */
	@RequestMapping("cg")
	public String getInstitution() {
		return "/cg";
	}

	/**
	 * 统计信息服务
	 * 
	 * @param zhibiao
	 * @param curlevel
	 * @param curcenterlng
	 * @param curcenterlat
	 * @param loctype
	 * @param session
	 * @return
	 */
	@RequestMapping("getSum")
	@ResponseBody
	public String getSum(String zhibiao, String curlevel, String curcenterlng, String curcenterlat, String loctype,
			HttpSession session) {
		session.setAttribute("curlevel", curlevel);
		session.setAttribute("curcenterlng", curcenterlng);
		session.setAttribute("curcenterlat", curcenterlat);
		return resultService.sumInfo(loctype);
	}

	@RequestMapping("getCount")
	@ResponseBody
	public String getCount(String quyu, String loctype, String zhibiao, HttpSession session) {
		Map<String, Long> metaData = resultService.getCount(quyu, loctype, zhibiao);
		String filename = "";
		try {
			filename = PieChart3D.generateRoChart(metaData, 600, 360, session, new PrintWriter("asdf"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String graphURL = "/Servlet/DisplayChart?filename=" + filename;
		return graphURL;
	}
	
	/**
	 * 统计信息服务 - 汇总
	 * 
	 * @return
	 */
	@RequestMapping("citySum")
	@ResponseBody
	public String getCitySum() {
		return resultService.getCitySum();
	}

	/**
	 * 内容搜索界面
	 */
	@RequestMapping("cgcx")
	public ModelAndView getCgcx() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("cgcx");
		mav.addObject("resultTypeList", resultService.findResultType());
		return mav;
	}

	/**
	 * 内容搜索服务
	 * 
	 * @param loctype
	 * @param cgmc
	 * @param ssdw
	 * @param cgmccxfs
	 * @param cglx
	 * @return
	 */
	@RequestMapping("getInstitution")
	@ResponseBody
	public String getResult(String loctype, String cgmc, String ssdw, String cgmccxfs, String cglx) {
		return resultService.contentSearch(loctype, cgmc, ssdw, cgmccxfs, cglx);
	}

	/**
	 * 获取alllist信息
	 * 
	 * @param loctype
	 * @return
	 */
	@RequestMapping("cgalllist")
	public ModelAndView getAllList(String loctype) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/cgalllist");
		mav.addObject("loctype", loctype);
		mav.addObject("zhibiao", "city");
		if (loctype.equals("city")) {
			mav.addObject("zhibiao", "所在市");
		} else if (loctype.equals("danwei")) {
			mav.addObject("zhibiao", "所属单位");
		}
		List<Object[]> ret = resultService.getAllList(loctype);
		if (ret != null) {
			mav.addObject("AllList", ret);
		}
		return mav;
	}

	/**
	 * 获取countlist信息
	 * 
	 * @param quyu
	 * @param loctype
	 * @param zhibiao
	 * @return
	 */
	@RequestMapping("cgcountlist")
	public ModelAndView getCountList(String quyu, String loctype, String zhibiao) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/cgcountlist");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = resultService.getCountList(quyu, loctype, zhibiao);
		if (ret != null) {
			mav.addObject("name", ret.get(0));
			mav.addObject("CountList", ret.get(1));
		}
		return mav;
	}

	/**
	 * 获取cx表1数据
	 * 
	 * @param yqmc
	 * @param ssdw
	 * @param yqmccxfs
	 * @param yqlx
	 * @param quyu
	 * @param loctype
	 * @return
	 */
	@RequestMapping("cgcxtable1")
	public ModelAndView getCxTable1Info(String cgmc, String ssdw, String cgmccxfs, String cglx, String quyu,
			String loctype) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/cgcxtable1");
		mav.addObject("tableInfoList", resultService.getCxTable1Info(cgmc, ssdw, cgmccxfs, cglx, quyu, loctype));
		return mav;
	}
	
	/**
	 * 获取表1数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("cgtable1")
	public ModelAndView getTable1Info(String zhibiao, String zhibiaomc, String quyu, String loctype, String page) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/cgtable1");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("zhibiaomc", zhibiaomc);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = resultService.getTable1Info(zhibiao, zhibiaomc, quyu, loctype, page);
		if (ret != null) {
			mav.addObject("totalpage", ret.get(0));
			mav.addObject("total", ret.get(1));
			mav.addObject("curpage", ret.get(2));
			mav.addObject("nextpage", ret.get(3));
			mav.addObject("lastpage", ret.get(4));
			mav.addObject("pagenum", ret.get(5));
			mav.addObject("beginnum", ret.get(6));
			mav.addObject("endnum", ret.get(7));
			mav.addObject("tableInfoList", ret.get(8));
		}
		return mav;
	}
	
	/**
	 * 获取表2数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("cgtable2")
	public ModelAndView getTable2Info(String id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/cgtable2");
		List<Object[]> ret = resultService.getTable2Info(id);
		if (ret != null) {
			Object[] xm = ret.get(0);
			mav.addObject("name", xm[0]);
			mav.addObject("type", xm[1]);
			mav.addObject("city", xm[2]);
			mav.addObject("county", xm[3]);
			mav.addObject("ins_name", xm[4]);
			mav.addObject("code", xm[5]);
		}
		return mav;
	}


}
